package com.twu.biblioteca;

import java.util.Collection;
import java.util.HashMap;

public class UserRepository {
    private HashMap<String, User> users;
    private User loggedInUser;

    public UserRepository() {
        users = new HashMap<String, User>();
    }

    public void addDefaultUsers() {
        addUser(new User("123-4567", "password", "Joe User", "joe@users.com", "1234 5678"));
        addUser(new User("555-5555", "555password", "Sally User", "sally@users.com", "8765 4321"));
        addUser(new User("098-7654", "thirdpassword", "Alex User", "alex@users.com", "1111 0000"));
    }

    public void addUser(User user) {
        users.put(user.getAccountNumber(), user);
    }

    public boolean logIn(String accountNumber, String password) {
        User user = users.get(accountNumber);

        if (user != null && user.getPassword().equals(password)) {
            loggedInUser = user;
            return true;
        }

        return false;
    }

    public void logOut() {
        loggedInUser = null;
    }

    public Collection<User> getUsers() {
        return users.values();
    }

    public User getLoggedInUser() {
        return loggedInUser;
    }
}
